﻿Public Class USEREDIT
    Dim connect As CONDB = CONDB.NewConnection()
    Private publicEmpid As Integer

    Public Sub New(empid As Integer)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        publicEmpid = empid
        Dim dt As DataTable = connect.GetTable("SELECT a.empid, a.cid, a.empcode, a.prename, b.ftprename, a.`name`, a.lname, a.nname, a.name_en, a.sex, c.sexdesc, a.`status`, a.f_cash, a.usreq FROM hospemp a LEFT JOIN masprename b ON a.prename = b.prename LEFT JOIN massex c ON a.sex = c.sex WHERE empid = '" & empid & "'")

        Try
            prenameTxt.Text = dt.Rows(0)("FTPRENAME")
            prenameTxt.Tag = dt.Rows(0)("PRENAME")
        Catch ex As Exception
            prenameTxt.Clear()
        End Try

        Try
            nameTxt.Text = dt.Rows(0)("NAME")
        Catch ex As Exception
            nameTxt.Clear()
        End Try

        Try
            LnameTxt.Text = dt.Rows(0)("LNAME")
        Catch ex As Exception
            LnameTxt.Clear()
        End Try

        Try
            nNameTxt.Text = dt.Rows(0)("NNAME")
        Catch ex As Exception
            nNameTxt.Clear()
        End Try

        Try
            name_enTxt.Text = dt.Rows(0)("NAME_EN")
        Catch ex As Exception
            name_enTxt.Clear()
        End Try

        Try
            sexTxt.Text = dt.Rows(0)("SEXDESC")
            sexTxt.Tag = dt.Rows(0)("SEX")
        Catch ex As Exception
            sexTxt.Clear()
        End Try

        Try
            CIDTxt.Text = dt.Rows(0)("CID")
        Catch ex As Exception
            CIDTxt.Clear()
        End Try

        Try
            empCodeTxt.Text = dt.Rows(0)("EMPCODE")
        Catch ex As Exception
            empCodeTxt.Clear()
        End Try

        Try
            usreqTxt.Text = dt.Rows(0)("USREQ")
            confirmUsreqTxt.Text = dt.Rows(0)("USREQ")
        Catch ex As Exception
            usreqTxt.Clear()
            confirmUsreqTxt.Clear()
        End Try
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    '-----------filter sub------------------
    Public Sub filterPrename()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(a.prename AS CHAR(8)) AS PRENAME, a.ftprename, b.sexdesc FROM masprename a LEFT JOIN massex b ON a.sex = b.sex WHERE a.status = '1'"
        s = New FILTERCLASS(prenameTxt, sql, "idคำนำหน้าชื่อ,คำนำหน้าชื่อ,เพศ", "50,160", "1,1", "1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Public Sub filterSex()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(sex AS CHAR(8)) as sex, sexdesc FROM massex WHERE `status` = '1'"
        s = New FILTERCLASS(sexTxt, sql, "id เพศ,เพศ", "50,120", "1,1", "1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    '--------clear after click---------
    Private Sub prenameTxt_Click(sender As Object, e As EventArgs) Handles prenameTxt.Click
        prenameTxt.Clear()
    End Sub

    Private Sub sexTxt_Click(sender As Object, e As EventArgs) Handles sexTxt.Click
        sexTxt.Clear()
    End Sub

    Private Sub USEREDIT_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        filterPrename()
        filterSex()
    End Sub

    Private Sub usreqTxt_TextChanged(sender As Object, e As EventArgs) Handles usreqTxt.TextChanged
        If usreqTxt.Text = confirmUsreqTxt.Text Then
            passwordMatch.Visible = False
            editHospemp.Enabled = True
        Else
            passwordMatch.Visible = True
            editHospemp.Enabled = False
        End If
    End Sub

    Private Sub confirmUsreqTxt_TextChanged(sender As Object, e As EventArgs) Handles confirmUsreqTxt.TextChanged
        If usreqTxt.Text = confirmUsreqTxt.Text Then
            passwordMatch.Visible = False
            editHospemp.Enabled = True
        Else
            passwordMatch.Visible = True
            editHospemp.Enabled = False
        End If
    End Sub

    Private Sub editHospemp_Click(sender As Object, e As EventArgs) Handles editHospemp.Click
        If prenameTxt.Text.ToString.Length > 0 And nameTxt.Text.ToString.Length > 0 And LnameTxt.Text.Length.ToString > 0 And nNameTxt.Text.Length.ToString > 0 And name_enTxt.Text.Length.ToString > 0 And sexTxt.Text.Length.ToString > 0 And CIDTxt.Text.Length.ToString > 0 And usreqTxt.Text.Length.ToString > 0 And confirmUsreqTxt.Text.Length.ToString > 0 Then
            Dim editStat As Integer
            editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลผู้ใช้ใช่หรือไม่", MsgBoxStyle.OkCancel)

            If editStat = 1 Then
                Dim objEditData As New USERGROUPCLASS
                objEditData.EMPID_ = publicEmpid
                objEditData.CID_ = CIDTxt.Text

                If empCodeTxt.Text Is Nothing Or empCodeTxt.Text = "" Then
                    objEditData.EMPCODE_ = 0
                Else
                    objEditData.EMPCODE_ = CInt(empCodeTxt.Text)
                End If

                objEditData.PRENAME_ = prenameTxt.Tag
                objEditData.NAME_ = nameTxt.Text
                objEditData.LNAME_ = LnameTxt.Text
                objEditData.NNAME_ = nNameTxt.Text
                objEditData.NAME_EN_ = name_enTxt.Text
                objEditData.SEX_ = sexTxt.Tag
                objEditData.F_CASH_ = f_cashStatusChk.Checked
                objEditData.USREQ_ = usreqTxt.Text
                objEditData.STATUS_ = hospempStatusChk.Checked

                If objEditData.editUser() = True Then
                    MsgBox("ท่านได้ทำการแก้ไขผู้ใช้เรียบร้อยแล้ว")
                    DialogResult = Windows.Forms.DialogResult.OK
                    Me.Close()
                End If
                connect.Dispose()
            Else
                MsgBox("โปรดกรุณาระบุชื่อกลุ่มซอฟต์แวร์")
            End If
        End If
    End Sub
End Class